package com.example.elderlycare.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elderlycare.controller.entity.TrainEmployeeRequestBody;
import com.example.elderlycare.dao.entity.Train;
import com.example.elderlycare.dao.entity.TrainEmployee;
import com.example.elderlycare.service.TrainEmployeeService;
import com.example.elderlycare.service.TrainService;
import com.example.elderlycare.service.entity.QueryListResponseBody;
import com.example.elderlycare.service.entity.TrainEmployeeItem;
import com.example.elderlycare.service.entity.TrainItem;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/train")
public class TrainController {
    @Resource
    TrainService trainService;

    @Resource
    TrainEmployeeService trainEmployeeService;

    @GetMapping
    public Result<QueryListResponseBody<TrainItem>> queryTrainRecord(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("name", name != null ? "%" + name + "%" : null);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        QueryListResponseBody<TrainItem> res = trainService.queryTrainList(params);
        if (res == null) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(res);
    }

    @PostMapping
    public Result<?> addTrainRecord(@Valid @RequestBody TrainEmployeeRequestBody.AddTrainRequestBody req) {
        Train train = Train.builder().
                trainName(req.getName()).
                startTime(req.getStartTime()).
                endTime(req.getEndTime()).
                address(req.getAddress()).
                content(req.getContent()).
                countNum(req.getCount()).
                trainer(req.getTrainer()).
                trainDepartment(req.getTrainDepartment()).
                trainTypeId(req.getTrainTypeId()).
                build();
        boolean success = trainService.save(train);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(train.getId());
    }

    @PutMapping
    public Result<?> updateTrainRecord(@Valid @RequestBody TrainEmployeeRequestBody.UpdateTrainRequestBody req) {
        Train train = Train.builder().
                id(req.getId()).
                trainName(req.getName()).
                startTime(req.getStartTime()).
                endTime(req.getEndTime()).
                address(req.getAddress()).
                content(req.getContent()).
                countNum(req.getCount()).
                trainer(req.getTrainer()).
                trainDepartment(req.getTrainDepartment()).
                trainTypeId(req.getTrainTypeId()).
                build();
        boolean success = trainService.updateById(train);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @DeleteMapping
    public Result<?> deleteTrainRecord(@Valid @RequestParam("id") Integer id) {
        boolean success = trainService.removeById(id);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @GetMapping("/employee")
    public Result<QueryListResponseBody<TrainEmployeeItem>> queryTrainUser(@RequestParam("id") Integer id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        QueryListResponseBody<TrainEmployeeItem> res = trainEmployeeService.queryTrainEmployeeList(params);
        if (res == null) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(res);
    }

    @PostMapping("/employee")
    public Result<?> addTrainUser(@Valid @RequestBody TrainEmployeeRequestBody.AddTrainEmployeeRequestBody req) {
        int count = 0;
        List<Integer> employee = req.getIds();
        for (Integer id : employee) {
            TrainEmployee trainEmployee = TrainEmployee.builder().
                    trainId(req.getTrainId()).
                    employeeId(id).
                    build();
            boolean success = trainEmployeeService.save(trainEmployee);
            if (!success) {
                count++;
            }
        }
        if (count != 0) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @DeleteMapping("/employee")
    public Result<?> deleteTrainUser(@Valid @RequestBody TrainEmployeeRequestBody.DeleteTrainEmployeeRequestBody req) {
        int count = 0;
        List<Integer> employee = req.getIds();
        for (Integer id : employee) {
            QueryWrapper<TrainEmployee> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("train_id", req.getTrainId());
            queryWrapper.eq("employee_id", id);
            boolean success = trainEmployeeService.remove(queryWrapper);
            if (!success) {
                count++;
            }
        }
        if (count != 0) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }
}
